package com.wf.ew.system.biz.impl;

import com.wf.ew.common.BaseDao;
import com.wf.ew.common.PageResult;
import com.wf.ew.common.utils.Utils;
import com.wf.ew.system.biz.DictionaryBiz;
import com.wf.ew.system.model.Dictionary;
import com.wf.ew.system.model.User;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2019-08-06 16:04:44
 * @version ：1.0
 */
@Repository
public class DictionaryBizimpl extends BaseDao implements DictionaryBiz {

	@Override
	public String list(PageResult<T> page, Dictionary dictionary) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Dictionary> rm = new BeanPropertyRowMapper<Dictionary>(Dictionary.class);

		StringBuffer sql = new StringBuffer("select d.id, d.dict_code, d.dict_name, d.description, d.state,"
			+" d.createTime, d.createUser, u.name createName, d.updateTime, d.updateUser"
			+ " from sys_dictionary d "
			+ " left join sys_user u on u.id=d.createUser "
				+" where d.del=0 ");
		if(Utils.isNotEmpty(dictionary)){
//			if(Utils.isNotEmpty(dictionary.getName())){
//				sql.append(" and instr(d.name,:name)>0 ");
//				map.put("name", dictionary.getName());
//			}
		}
		return jsonPage(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public Dictionary getById(Dictionary dictionary) {
		RowMapper<Dictionary> rm = new BeanPropertyRowMapper<Dictionary>(Dictionary.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select d.id, d.dict_code, d.dict_name, d.description, d.state,"
			+" d.createTime, d.createUser, d.updateTime, d.updateUser "
			+ "from sys_dictionary d where d.del=0 ");
		if(Utils.isNotEmpty(dictionary)){
			if(Utils.isNotEmpty(dictionary.getId())){
				sql.append("and d.id=:id ");
				map.put("id", dictionary.getId());
			}
			if(Utils.isNotEmpty(dictionary.getDict_code())){
				sql.append("and d.dict_code=:dict_code ");
				map.put("dict_code", dictionary.getDict_code());
			}
		}
		getMapParameter(sql, map);
		try{
            return jdbcTemplate.queryForObject(sql.toString(), map, rm);
        }catch (Exception e){
            return null;
        }
	}

	@Override
	public boolean add(Dictionary dictionary, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into sys_dictionary (dict_code,dict_name,description,state "
			+",createUser )");
		sql.append("values (:dict_code,:dict_name,:description,:state "
			+",:createUser )");
		map.put("dict_code", dictionary.getDict_code()==null?null:dictionary.getDict_code()+"");
		map.put("dict_name", dictionary.getDict_name());
		map.put("description", dictionary.getDescription());
		map.put("state", dictionary.getState());
		map.put("createUser", loginUser.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(Dictionary dictionary, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update sys_dictionary set dict_code=:dict_code ");
		sql.append(", dict_name=:dict_name, description=:description, state=:state");
		sql.append(", updateUser=:updateUser, updateTime=now() where id=:id");
		map.put("dict_code", dictionary.getDict_code()==null?null:dictionary.getDict_code()+"");
		map.put("dict_name", dictionary.getDict_name());
		map.put("description", dictionary.getDescription());
		map.put("state", dictionary.getState());
		map.put("updateUser", loginUser.getId());
		map.put("id", dictionary.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean delete(Dictionary dictionary, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(dictionary.getIds().length()>0){
			for(String ID : dictionary.getIds().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update sys_dictionary set del=1" +
						",updateUser=:updateUser, updateTime=now() " 
						+ " where id =:id ");
				map.put("updateUser", loginUser.getId());
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
